package com.longrise.android.byjk.plugins.dealsituation.course.video2.study;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by godliness on 2017/5/26.
 */

public class MobileNetWorkBroadCast extends BroadcastReceiver {

    private NetWorkStateCallback mCallback;

    public MobileNetWorkBroadCast(NetWorkStateCallback callback) {
        this.mCallback = callback;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
            return;
        }
        NetworkInfo wifiNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiNetworkInfo != null) {
            wifiState = wifiNetworkInfo.getState();
        }
        NetworkInfo mobileNetWorkInfor = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileNetWorkInfor != null) {
            mobileState = mobileNetWorkInfor.getState();
        }
        if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState && NetworkInfo.State.CONNECTED == mobileState) {
            if (mCallback != null) {
                mCallback.mobileState();
            }
        } else if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
        } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
            if (mCallback != null) {
                mCallback.wifiState();
            }
        }
    }

    public interface NetWorkStateCallback {
        void mobileState();

        void wifiState();
    }
}
